package com.example.android.BluetoothChat;

import java.util.*;

public class CubeMessageParser {
	
	String buffer = "";
	HashMap<String, String> variables = new HashMap<String, String>();
	
	public String getString(String name) {
		if (variables.containsKey(name)) {
			return variables.get(name);
		}
		return "";
	}

	public Float getFloat(String name) {
		try {
			return Float.valueOf(variables.get(name));
		} catch(Exception e) {
			return -1.0f;
		}
	}

	public int getInt(String name) {
		try {
			return Integer.valueOf(variables.get(name));
		} catch(Exception e) {
			return -1;
		}
	}
	
	public CubeMessageParser(BluetoothChat bluetoothChat) {
		
	}
	
	public void append(String message) {
		buffer += message.replace(" ", "").replace("\r", "");
		
		int row_length;
		while ((row_length = buffer.indexOf("\n")) >= 0) {
			String row = buffer.substring(0, row_length);
			buffer = buffer.substring(row_length + 1);

			String[] split_buffer = row.split("=");
			if (split_buffer.length == 2) {
				String name;
				String value;
				name = split_buffer[0];
				value = split_buffer[1];
				variables.put(name, value);
			}
		}
	}
};